#!/bin/bash

set -euo pipefail

# Check if prettier is installed
if ! command -v prettier >/dev/null 2>&1; then
  # If it's not installed, for now skip autoformatting. We can make it a tool dependency
  # in the future, if we want.
  echo "Prettier not found. Skipping..."
  exit 0
fi

FILES=$(git diff --cached --name-only --diff-filter=ACMR -- '*.yml' '*.yaml' '*.md' '*.mdx' '*.html' '*.css' | sed 's| |\\ |g' | (grep -v '^external' || true))
[ -z "$FILES" ] && exit 0

# Prettify all selected files
echo "$FILES" | xargs prettier --ignore-unknown --write

# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add

exit 0
